const app = getApp();
const utils = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: "",
    friendId: 0, //输入的绑定的Id
    isbind: false, //是否有绑定过
    bindInfo: null, //绑定过的信息
    bindList: [] //绑定我的好友
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getBindMyList();
  },


  //获取绑定好友
  getBindMyList: function() {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetBindMyList?uid=' + app.globalData.userInfo.Uid,
      success: function(res) {
        if (res.data.Code == 0) {
          let bindInfo = res.data.Data.BindInfo;
          if (bindInfo) {
            bindInfo.Uid = bindInfo.ToUid;
            bindInfo.Avater = utils.formatAvater(bindInfo.Avater, 'https://static.711688.net.cn/upload/card/source/');
            if (bindInfo.ExpireTime){
              bindInfo.ExpireTime = bindInfo.ExpireTime.substr(0, 10);
            }
            that.setData({
              isbind: true,
              bindInfo: bindInfo
            })
          }else{
            that.setData({
              isbind: false,
              bindInfo: bindInfo
            })
          }
          let bindList = res.data.Data.BindList;
          if (bindList) {
            for (let i = 0; i < bindList.length; i++) {
              bindList[i].Avater = utils.formatAvater(bindList[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
              if (bindList[i].ExpireTime){
                bindList[i].ExpireTime = bindList[i].ExpireTime.substr(0, 10)
              }
              if (bindList[i].CreateTime){
                bindList[i].CreateTime = bindList[i].CreateTime.substr(0, 10)
              }
            }
          }
          that.setData({
            bindList: bindList
          })
        }
      }
    })
  },

  //获取绑定好友信息
  getBindFriendInfo: function() {
    let that = this;
    if (!that.data.friendId){
       wx.showModal({
         title: '提示',
         content: '请输入绑定好友ID',
         showCancel: false
       })
       return;
    }
    if (that.data.friendId == app.globalData.userInfo.Uid){
      wx.showModal({
        title: '提示',
        content: '不能自己绑定自己!',
        showCancel: false
      })
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + that.data.friendId,
      success: function(res) {
        wx.hideLoading();
        if (res.data.Code == 0) {
          if (res.data.Data) {
            let bindInfo = res.data.Data;
            bindInfo.Avater = utils.formatAvater(bindInfo.Avater, 'https://static.711688.net.cn/upload/card/source/');
            that.setData({
              bindInfo: bindInfo
            })
          } else {
            wx.showModal({
              title: '提示',
              content: "该成员还未创建玖玖客！",
              showCancel: false
            })
            return
          }
        }
      },
    })
  },

  //申请绑定好友
  applyBindFriend: function() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认申请绑定!',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host +  '/api/Staff/AddBindUser',
            data: {
              fromUid: app.globalData.userInfo.Uid,
              toUid: that.data.bindInfo.Uid,
              expireTime: that.data.date
            },
            method: 'POST',
            success: function(res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '绑定成功',
                })
                that.getBindMyList();
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false,
                })
              }
            },
            fail: function(err) {
              wx.showModal({
                title: '提示',
                content: err.data.Message,
                showCancel: false,
              })
            }
          })
        }
      }
    })
  },

  //接受或取消
  setBindStatus: function(id, status) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认!',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + '/api/Staff/SetBindStatus',
            data: {
              id: id,
              status: status?2:1,
              uid:app.globalData.userInfo.Uid
            },
            method: 'POST',
            success: function(res) {
              if (res.data.Code == 0) {
                that.getBindMyList();
                wx.showToast({
                  title: '成功',
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.Message,
                  showCancel: false,
                })
              }
            },
            fail: function(err) {
              wx.showModal({
                title: '提示',
                content: err.data.Message,
                showCancel: false,
              })
            }
          })
        }
      }
    })

  },

  //接受
  bindaccept: function(e) {
    let {
      id,
      status
    } = e.currentTarget.dataset;
    this.setBindStatus(id, status)
  },

  //取消
  bindcancel: function(e) {
    let {
      id,
      status
    } = e.currentTarget.dataset;
    this.setBindStatus(id, status)
  },

  getbindinput: function(e) {
    this.setData({
      friendId: e.detail.value
    })
  },

  getbindblur: function(e) {
    this.setData({
      friendId: e.detail.value
    })
    this.getBindFriendInfo();
  },

  bindDateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },

  // 同步名片、产品、官网
  aiSyn: function(e){
    wx.showLoading({
      title: '正在同步',
      mask: true,
    })
    let that = this;
    var type = e.currentTarget.dataset.syntype;
    wx.request({
      url: app.globalData.host + '/api/Staff/SynBindData',
      data:{
        uid: app.globalData.userInfo.Uid,
        type:type
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        if(res.data.Code == 0){
          wx.showToast({
            title: '同步成功',
          })
          that.getBindMyList();
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  }
})